package flc.ast.adapter;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.stark.midi.lib.model.MidFileBean;

import java.util.List;

import flc.ast.R;
import flc.ast.bean.MyCollectBean;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.utils.MathUtil;

public class MusicAdapter extends StkProviderMultiAdapter<MidFileBean> {

    private List<Integer> hotIconList;

    public MusicAdapter() {
        addItemProvider(new MusicItemProvider());
    }

    public void setIconList(List<Integer> iconList) {
        hotIconList = iconList;
    }

    public List<Integer> getHotIconList() {
        return hotIconList;
    }

    private class MusicItemProvider extends BaseItemProvider<MidFileBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_music;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MidFileBean midFileBean) {
            baseViewHolder.setText(R.id.tvMusicItemName, midFileBean.getFileName());

            if (hotIconList == null || baseViewHolder.getBindingAdapterPosition() >= hotIconList.size()) {
                baseViewHolder.setImageResource(R.id.ivMusicItemImg, R.drawable.w1);
            } else {
                int position = baseViewHolder.getBindingAdapterPosition();
                baseViewHolder.setImageResource(R.id.ivMusicItemImg, hotIconList.get(position));
            }
            List<MyCollectBean> songCollectionList = AppSpUtils.getSongCollectionList();
            if (songCollectionList != null && songCollectionList.size() != 0) {
                for (MyCollectBean myCollectBean : songCollectionList) {
                    if (myCollectBean.getMidFileBean().equals(midFileBean)){
                        midFileBean.setSelected(true);
                        break;
                    }else {
                        midFileBean.setSelected(false);
                    }
                }
            }else {
                midFileBean.setSelected(false);
            }
            if (midFileBean.isSelected()) {
                baseViewHolder.setImageResource(R.id.ivMusicItemCollect, R.drawable.sc1);
            } else {
                baseViewHolder.setImageResource(R.id.ivMusicItemCollect, R.drawable.wsc1);
            }
        }
    }
}
